﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace MapViewer
{
    [ToolboxItem(true)]
    public class MgDefaultToolbar : ToolStrip, IDefaultToolbar
    {
        public MgDefaultToolbar()
            : base()
        {
            this.GripStyle = ToolStripGripStyle.Visible;
            this.ZoomIn = new ToolStripButton("", Properties.Resources.icon_zoomin, OnZoomIn)
            {
                ToolTipText = Properties.Resources.TextZoomIn,
                DisplayStyle = ToolStripItemDisplayStyle.Image
            };
            this.ZoomOut = new ToolStripButton("", Properties.Resources.icon_zoomout, OnZoomOut)
            {
                ToolTipText = Properties.Resources.TextZoomOut,
                DisplayStyle = ToolStripItemDisplayStyle.Image
            };
            this.ZoomExtents = new ToolStripButton("", Properties.Resources.icon_fitwindow, OnZoomExtents)
            {
                ToolTipText = Properties.Resources.TextZoomExtents,
                DisplayStyle = ToolStripItemDisplayStyle.Image
            };
            this.Select = new ToolStripButton("", Properties.Resources.icon_select, OnSelect)
            {
                ToolTipText = Properties.Resources.TextSelect,
                DisplayStyle = ToolStripItemDisplayStyle.Image
            };
            this.Pan = new ToolStripButton("", Properties.Resources.icon_pan, OnPan)
            {
                ToolTipText = Properties.Resources.TextPan,
                DisplayStyle = ToolStripItemDisplayStyle.Image
            };
            this.ClearSelection = new ToolStripButton("", Properties.Resources.icon_clearselect, OnClearSelection)
            {
                ToolTipText = Properties.Resources.TextClearSelection,
                DisplayStyle = ToolStripItemDisplayStyle.Image
            };
            this.RefreshMap = new ToolStripButton("", Properties.Resources.icon_refreshmap, OnRefreshMap)
            {
                ToolTipText = Properties.Resources.TextRefreshMap,
                DisplayStyle = ToolStripItemDisplayStyle.Image
            };
            this.ToggleTooltips = new ToolStripButton(Properties.Resources.TextDisableTooltips, Properties.Resources.ui_tooltip_balloon_bottom, OnToggleTooltips)
            {
                ToolTipText = Properties.Resources.TextDisableTooltips,
                DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
            };
            this.Loading = new ToolStripButton("", Properties.Resources.icon_loading)
            {
                Alignment = ToolStripItemAlignment.Right,
                ImageScaling = ToolStripItemImageScaling.None,
                Visible = false
            };

            this.Items.AddRange(new ToolStripItem[]
            {
                ZoomIn,
                ZoomOut,
                ZoomExtents,
                new ToolStripSeparator(),
                Select,
                Pan,
                new ToolStripSeparator(),
                ClearSelection,
                RefreshMap,
                new ToolStripSeparator(),
                ToggleTooltips,
                Loading
            });
        }

        private IMapViewer _viewer;

        public IMapViewer Viewer
        {
            get { return _viewer; }
            set
            {
                _viewer = value;
                TooltipStateChanged();
                UpdateButtonCheckedState();
            }
        }

        internal ToolStripButton Loading { get; private set; }

        internal ToolStripButton ZoomIn { get; private set; }

        void OnZoomIn(object sender, EventArgs e)
        {
            if (_viewer == null)
                return;

            _viewer.ActiveTool = MapActiveTool.ZoomIn;
            _viewer.PropertyChanged += OnViewerPropertyChanged;
        }

        internal ToolStripButton ZoomOut { get; private set; }

        void OnZoomOut(object sender, EventArgs e)
        {
            if (_viewer == null)
                return;

            _viewer.ActiveTool = MapActiveTool.ZoomOut;
        }

        internal ToolStripButton ZoomExtents { get; private set; }

        void OnZoomExtents(object sender, EventArgs e)
        {
            if (_viewer == null)
                return;

            _viewer.InitialMapView();
        }

        internal ToolStripButton Select { get; private set; }

        void OnSelect(object sender, EventArgs e)
        {
            if (_viewer == null)
                return;

            _viewer.ActiveTool = MapActiveTool.Select;
        }

        internal ToolStripButton Pan { get; private set; }

        void OnPan(object sender, EventArgs e)
        {
            if (_viewer == null)
                return;

            _viewer.ActiveTool = MapActiveTool.Pan;
        }

        internal ToolStripButton ClearSelection { get; private set; }

        void OnClearSelection(object sender, EventArgs e)
        {
            if (_viewer == null)
                return;

            _viewer.ClearSelection();
        }

        internal ToolStripButton RefreshMap { get; private set; }

        void OnRefreshMap(object sender, EventArgs e)
        {
            if (_viewer == null)
                return;

            _viewer.RefreshMap();
        }

        internal ToolStripButton ToggleTooltips { get; private set; }

        void OnToggleTooltips(object sender, EventArgs e)
        {
            if (_viewer == null)
                return;

            _viewer.FeatureTooltipsEnabled = !_viewer.FeatureTooltipsEnabled;
            TooltipStateChanged();
        }

        private void TooltipStateChanged()
        {
            if (_viewer == null)
                return;

            if (_viewer.FeatureTooltipsEnabled)
            {
                this.ToggleTooltips.Text = Properties.Resources.TextDisableTooltips;
                this.ToggleTooltips.ToolTipText = Properties.Resources.TooltipDisableTooltips;
            }
            else
            {
                this.ToggleTooltips.Text = Properties.Resources.TextEnableTooltips;
                this.ToggleTooltips.ToolTipText = Properties.Resources.TooltipEnableTooltips;
            }
        }

        private void UpdateButtonCheckedState()
        {
            var at = (_viewer == null) ? MapActiveTool.None : _viewer.ActiveTool;
            this.Pan.Checked = (at == MapActiveTool.Pan);
            this.Select.Checked = (at == MapActiveTool.Select);
            this.ZoomIn.Checked = (at == MapActiveTool.ZoomIn);
            this.ZoomOut.Checked = (at == MapActiveTool.ZoomOut);
        }

        private void OnViewerPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsBusy")
            {
                var busy = _viewer.IsBusy;
                this.ZoomExtents.Enabled = this.ZoomIn.Enabled
                                       = this.ZoomOut.Enabled
                                       = this.ClearSelection.Enabled
                                       = this.Pan.Enabled
                                       = this.Select.Enabled
                                       = this.RefreshMap.Enabled = !busy;
                this.Loading.Visible = busy;
            }
            else if (e.PropertyName == "ActiveTool" || e.PropertyName == "DigitizingType")
            {
                UpdateButtonCheckedState();
            }
        }
    }
}
